const fs = require('fs');
const path = require('path');

/**
 * 递归获取文件路径信息
 * @param {*} pathArr 
 * @param {*} getFiles 
 * @param {*} rootPath 
 * @param {*} filePath 
 */
function getFilePathDeep(pathArr, getFiles, rootPath, filePath) {
  if (getFiles <= 0) {
    return false;
  } else {
    getFiles.forEach(item => {
      let crrentPath = path.join(filePath, item);
      if (fs.existsSync(filePath) && !fs.statSync(crrentPath).isFile()) {
        let getDeepFiles = fs.readdirSync(crrentPath, 'utf-8');
        getFilePathDeep(pathArr, getDeepFiles, rootPath + item + "/", crrentPath);
      } else {
        pathArr.push(rootPath + item);
      }
    });
  }

}


/**
 * 获取当前文件对应的路径信息
 * @param {*} pathArr 
 * @param {*} filePath 
 * @param {*} rootPath 
 */
module.exports = function getFilePath(pathArr, filePath, rootPath = "/") {
  if (fs.existsSync(filePath) && !fs.statSync(filePath).isFile()) {
    let getFiles = fs.readdirSync(filePath, 'utf-8');
    if (getFiles) {
      getFilePathDeep(pathArr, getFiles, rootPath, filePath);
    }
  }
}